﻿<%@ WebHandler Language="C#" Class="GetTreeData" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;


public class GetTreeData : IHttpHandler
{
    private HsTec.Mobile.Green.BLL.ReportTemplateBLL bll = new HsTec.Mobile.Green.BLL.ReportTemplateBLL();

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string sId = context.Request.Form["id"];
        string sState = context.Request.Form["checkstate"];
        if (String.IsNullOrEmpty(sId))
        {
            TreeItem tdata = new TreeItem() { id = "0", checkstate = 0, complete = true, isexpand = true, showcheck = true, text = "自定义数据表", value = "TemplateTree_Right.aspx?id=0", hasChildren = true };
            tdata.ChildNodes = new List<TreeItem>();
            List<HsTec.Mobile.Green.Model.ReportTemplate> listRoot = bll.GetModelListByID(0);
            foreach (HsTec.Mobile.Green.Model.ReportTemplate rt in listRoot)
            {
                //var ti0 = new TreeItem()
                //{
                //    id = rt.RT_ID.ToString(),
                //    value = "TemplateTree_Right.aspx?id=" + rt.RT_ID.ToString(),
                //    text = rt.FieldAlias,
                //    isexpand = false,
                //    hasChildren = rt.FieldChildCount > 0,
                //    complete = false
                //};
                TreeItem ti0 = new TreeItem();
                ti0.id = rt.RT_ID.ToString();
                ti0.value = "TemplateTree_Right.aspx?id=" + rt.RT_ID.ToString();
                ti0.text = rt.FieldAlias;
                ti0.isexpand = false;
                ti0.hasChildren = rt.FieldChildCount > 0;
                ti0.complete = false;
                tdata.ChildNodes.Add(ti0);
            }
            context.Response.Write(tdata.ToJsonString());
        }
        else
        {
            List<TreeItem> list = new List<TreeItem>();
            List<HsTec.Mobile.Green.Model.ReportTemplate> listNode = bll.GetModelListByID(int.Parse(sId));
            foreach (HsTec.Mobile.Green.Model.ReportTemplate rt in listNode)
            {
                //var ti0 = new TreeItem()
                //{
                //    id = rt.RT_ID.ToString(),
                //    value = "TemplateTree_Right.aspx?id=" + rt.RT_ID.ToString(),
                //    text = rt.FieldAlias,
                //    isexpand = false,
                //    hasChildren = rt.FieldChildCount > 0,
                //    complete = false
                //}
                TreeItem ti0 = new TreeItem();
                ti0.id = rt.RT_ID.ToString();
                ti0.value = "TemplateTree_Right.aspx?id=" + rt.RT_ID.ToString();
                ti0.text = rt.FieldAlias;
                ti0.isexpand = false;
                ti0.hasChildren = rt.FieldChildCount > 0;
                ti0.complete = false;
                list.Add(ti0);
            }
            context.Response.Write(this.ListToJson(list));
        }
    }

    private string ListToJson(List<TreeItem> list)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        string sJson = String.Empty;
        foreach (TreeItem ti in list)
        {
            if (String.IsNullOrEmpty(sJson))
                sJson = jss.Serialize(ti);
            else
                sJson += "," + jss.Serialize(ti);
        }
        return "[" + sJson + "]";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}